<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :lock-scroll="true"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @open="openForm"
    width="720px"
    top="40px"
  >
    <el-form :model="admin" :rules="rules" labelPosition="top" ref="form" class="custom-form">
      <el-form-item label="用户名" prop="userName">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-input v-model="admin.userName" placeholder="请输入用户名"></el-input>
          </el-col>
          <el-col :span="14">
            <span>输入系统登录账号，至少2个字符</span>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-input v-model="admin.nickName" placeholder="请输入昵称"></el-input>
          </el-col>
          <el-col :span="14">

          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="账户密码" prop="password">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-input type="password" v-model="admin.password" placeholder="请输入用户密码"></el-input>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item label="重复密码" prop="repassword">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-input type="password" v-model="admin.repassword" placeholder="请再次输入用户密码"></el-input>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item label="邮箱地址" prop="email">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-input v-model="admin.email" placeholder="请输入邮箱地址"></el-input>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button type="primary" @click="submitForm">创建</el-button>
      <el-button @click="restForm">重置</el-button>
    </div>
  </el-dialog>

</template>
<script>
  import Dialog from 'src/components/common/js/dialog-mix'
  import AdminService from 'src/service/admin'

  export default {
    name: 'CreateAdmin',
    mixins: [Dialog],
    data() {
      const checkPassword = (rule, value, callback) => {
        if (value == null) {
          callback(new Error('请再次输入密码!'))
        } else {
          if (this.admin.password !== value) {
            callback(new Error('两次输入的密码不一致!'))
          } else {
            callback()
          }
        }
      }
      return {
        admin: {
          id: null,
          status: 1,
          userName: '',
          nickName: '',
          type: 1,
          birthday: '',
          sex: '',
          email: '',
          qq: '',
          weixin: '',
          figureUrl: '',
          repassword: ''
        },
        rules: {
          userName: [
            {required: true, message: '请输入用户名称', trigger: 'blur'},
            {min: 2, message: '最少2个字符', trigger: 'blur'}
          ],
          nickName: [
            {required: true, message: '请输入昵称', trigger: 'blur'},
            {min: 2, message: '最少2个字符', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入用户密码', trigger: 'blur'}

          ],
          repassword: [
            {required: true, message: '请再次输入密码', trigger: 'blur'},
            {validator: checkPassword, trigger: 'blur'}
          ],
          email: [
            {required: true, message: '请输入邮箱地址', trigger: 'blur'},
            {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change'}]
        }
      }
    },
    methods: {
      openForm: function () {
        const me = this
        this.initData()
        setTimeout(function () {
          me.restForm()
        }, 600)
      },
      initData: function () {
        this.admin = {
          id: null,
          status: 1,
          userName: '',
          nickName: '',
          type: 1,
          birthday: '',
          sex: '',
          email: '',
          qq: '',
          weixin: '',
          figureUrl: '',
          repassword: ''
        }
      },
      submitForm: function () {
        const me = this
        this.$refs['form'].validate((valid) => {
          if (valid) {
            AdminService.createAdmin(this.admin).then(function (data) {
              if (data != null) {
                const admin = data.datas[0]
                me.$message.success('创建管理员成功!')
                me.$emit('created', admin)
                me.initData()
              } else {
                me.$message.error('创建管理员失败!')
              }
            })
          } else {
            return false
          }
        })
      },
      restForm: function () {
        this.$refs['form'].resetFields()
      }
    }
  }
</script>
